
<!DOCTYPE HTML>
<html lang="zh-hans" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>Getters · Gitbook demo</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.2">
        <meta name="author" content="lanyue">
        
        
    
    <link rel="stylesheet" href="gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="gitbook/gitbook-plugin-highlight/website.css">
                
            
                
                <link rel="stylesheet" href="gitbook/gitbook-plugin-search/search.css">
                
            
                
                <link rel="stylesheet" href="gitbook/gitbook-plugin-fontsettings/website.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    
        
    

        
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="mutations.html" />
    
    
    <link rel="prev" href="state.html" />
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="输入并搜索" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    

    

    
        
        
    
        <li class="chapter " data-level="1.1" data-path="./">
            
                <a href="./">
            
                    
                    Introduction
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2" >
            
                <a target="_blank" href="https://github.com/vuejs/vuex/tree/1.0/docs">
            
                    
                    寻找 1.0 文档？
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3" >
            
                <a target="_blank" href="https://github.com/vuejs/vuex/releases">
            
                    
                    更新记录
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4" data-path="installation.html">
            
                <a href="installation.html">
            
                    
                    安装
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5" data-path="intro.html">
            
                <a href="intro.html">
            
                    
                    Vuex 是什么？
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6" data-path="getting-started.html">
            
                <a href="getting-started.html">
            
                    
                    开始
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7" >
            
                <span>
            
                    
                    核心概念
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.7.1" data-path="state.html">
            
                <a href="state.html">
            
                    
                    State
            
                </a>
            

            
        </li>
    
        <li class="chapter active" data-level="1.7.2" data-path="getters.html">
            
                <a href="getters.html">
            
                    
                    Getters
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.3" data-path="mutations.html">
            
                <a href="mutations.html">
            
                    
                    Mutations
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.4" data-path="actions.html">
            
                <a href="actions.html">
            
                    
                    Actions
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.5" data-path="modules.html">
            
                <a href="modules.html">
            
                    
                    Modules
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.8" data-path="structure.html">
            
                <a href="structure.html">
            
                    
                    项目结构
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.9" data-path="plugins.html">
            
                <a href="plugins.html">
            
                    
                    插件
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.10" data-path="strict.html">
            
                <a href="strict.html">
            
                    
                    严格模式
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.11" data-path="forms.html">
            
                <a href="forms.html">
            
                    
                    表单处理
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.12" data-path="testing.html">
            
                <a href="testing.html">
            
                    
                    测试
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.13" data-path="hot-reload.html">
            
                <a href="hot-reload.html">
            
                    
                    热重载
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.14" data-path="api.html">
            
                <a href="api.html">
            
                    
                    API 文档
            
                </a>
            

            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            本书使用 GitBook 发布
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="." >Getters</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <h1 id="getters">Getters</h1>
<p>&#x6709;&#x65F6;&#x5019;&#x6211;&#x4EEC;&#x9700;&#x8981;&#x4ECE; store &#x4E2D;&#x7684; state &#x4E2D;&#x6D3E;&#x751F;&#x51FA;&#x4E00;&#x4E9B;&#x72B6;&#x6001;&#xFF0C;&#x4F8B;&#x5982;&#x5BF9;&#x5217;&#x8868;&#x8FDB;&#x884C;&#x8FC7;&#x6EE4;&#x5E76;&#x8BA1;&#x6570;&#xFF1A;</p>
<pre><code class="lang-js">computed: {
  doneTodosCount () {
    <span class="hljs-keyword">return</span> <span class="hljs-keyword">this</span>.$store.state.todos.filter(todo =&gt; todo.done).length
  }
}
</code></pre>
<p>&#x5982;&#x679C;&#x6709;&#x591A;&#x4E2A;&#x7EC4;&#x4EF6;&#x9700;&#x8981;&#x7528;&#x5230;&#x6B64;&#x5C5E;&#x6027;&#xFF0C;&#x6211;&#x4EEC;&#x8981;&#x4E48;&#x590D;&#x5236;&#x8FD9;&#x4E2A;&#x51FD;&#x6570;&#xFF0C;&#x6216;&#x8005;&#x62BD;&#x53D6;&#x5230;&#x4E00;&#x4E2A;&#x5171;&#x4EAB;&#x51FD;&#x6570;&#x7136;&#x540E;&#x5728;&#x591A;&#x5904;&#x5BFC;&#x5165;&#x5B83; &#x2014;&#x2014; &#x65E0;&#x8BBA;&#x54EA;&#x79CD;&#x65B9;&#x5F0F;&#x90FD;&#x4E0D;&#x662F;&#x5F88;&#x7406;&#x60F3;&#x3002;</p>
<p>Vuex &#x5141;&#x8BB8;&#x6211;&#x4EEC;&#x5728; store &#x4E2D;&#x5B9A;&#x4E49;&#x300E;getters&#x300F;&#xFF08;&#x53EF;&#x4EE5;&#x8BA4;&#x4E3A;&#x662F; store &#x7684;&#x8BA1;&#x7B97;&#x5C5E;&#x6027;&#xFF09;&#x3002;Getters &#x63A5;&#x53D7; state &#x4F5C;&#x4E3A;&#x5176;&#x7B2C;&#x4E00;&#x4E2A;&#x53C2;&#x6570;&#xFF1A;</p>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> store = <span class="hljs-keyword">new</span> Vuex.Store({
  state: {
    todos: [
      { id: <span class="hljs-number">1</span>, text: <span class="hljs-string">&apos;...&apos;</span>, done: <span class="hljs-literal">true</span> },
      { id: <span class="hljs-number">2</span>, text: <span class="hljs-string">&apos;...&apos;</span>, done: <span class="hljs-literal">false</span> }
    ]
  },
  getters: {
    doneTodos: state =&gt; {
      <span class="hljs-keyword">return</span> state.todos.filter(todo =&gt; todo.done)
    }
  }
})
</code></pre>
<p>Getters &#x4F1A;&#x66B4;&#x9732;&#x4E3A; <code>store.getters</code> &#x5BF9;&#x8C61;&#xFF1A;</p>
<pre><code class="lang-js">store.getters.doneTodos <span class="hljs-comment">// -&gt; [{ id: 1, text: &apos;...&apos;, done: true }]</span>
</code></pre>
<p>Getters &#x4E5F;&#x53EF;&#x4EE5;&#x63A5;&#x53D7;&#x5176;&#x4ED6; getters &#x4F5C;&#x4E3A;&#x7B2C;&#x4E8C;&#x4E2A;&#x53C2;&#x6570;&#xFF1A;</p>
<pre><code class="lang-js">getters: {
  <span class="hljs-comment">// ...</span>
  doneTodosCount: (state, getters) =&gt; {
    <span class="hljs-keyword">return</span> getters.doneTodos.length
  }
}
</code></pre>
<pre><code class="lang-js">store.getters.doneTodosCount <span class="hljs-comment">// -&gt; 1</span>
</code></pre>
<p>&#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x5F88;&#x5BB9;&#x6613;&#x5730;&#x5728;&#x4EFB;&#x4F55;&#x7EC4;&#x4EF6;&#x4E2D;&#x4F7F;&#x7528;&#x5B83;&#xFF1A;</p>
<pre><code class="lang-js">computed: {
  doneTodosCount () {
    <span class="hljs-keyword">return</span> <span class="hljs-keyword">this</span>.$store.getters.doneTodosCount
  }
}
</code></pre>
<h3 id="mapgetters-&#x8F85;&#x52A9;&#x51FD;&#x6570;"><code>mapGetters</code> &#x8F85;&#x52A9;&#x51FD;&#x6570;</h3>
<p><code>mapGetters</code> &#x8F85;&#x52A9;&#x51FD;&#x6570;&#x4EC5;&#x4EC5;&#x662F;&#x5C06; store &#x4E2D;&#x7684; getters &#x6620;&#x5C04;&#x5230;&#x5C40;&#x90E8;&#x8BA1;&#x7B97;&#x5C5E;&#x6027;&#xFF1A;</p>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> { mapGetters } <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;vuex&apos;</span>

<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> {
  <span class="hljs-comment">// ...</span>
  computed: {
  <span class="hljs-comment">// &#x4F7F;&#x7528;&#x5BF9;&#x8C61;&#x5C55;&#x5F00;&#x8FD0;&#x7B97;&#x7B26;&#x5C06; getters &#x6DF7;&#x5165; computed &#x5BF9;&#x8C61;&#x4E2D;</span>
    ...mapGetters([
      <span class="hljs-string">&apos;doneTodosCount&apos;</span>,
      <span class="hljs-string">&apos;anotherGetter&apos;</span>,
      <span class="hljs-comment">// ...</span>
    ])
  }
}
</code></pre>
<p>&#x5982;&#x679C;&#x4F60;&#x60F3;&#x5C06;&#x4E00;&#x4E2A; getter &#x5C5E;&#x6027;&#x53E6;&#x53D6;&#x4E00;&#x4E2A;&#x540D;&#x5B57;&#xFF0C;&#x4F7F;&#x7528;&#x5BF9;&#x8C61;&#x5F62;&#x5F0F;&#xFF1A;</p>
<pre><code class="lang-js">mapGetters({
  <span class="hljs-comment">// &#x6620;&#x5C04; this.doneCount &#x4E3A; store.getters.doneTodosCount</span>
  doneCount: <span class="hljs-string">&apos;doneTodosCount&apos;</span>
})
</code></pre>

                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="state.html" class="navigation navigation-prev " aria-label="Previous page: State">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="mutations.html" class="navigation navigation-next " aria-label="Next page: Mutations">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"Getters","level":"1.7.2","depth":2,"next":{"title":"Mutations","level":"1.7.3","depth":2,"path":"mutations.md","ref":"mutations.md","articles":[]},"previous":{"title":"State","level":"1.7.1","depth":2,"path":"state.md","ref":"state.md","articles":[]},"dir":"ltr"},"config":{"plugins":[],"root":"./","styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"pluginsConfig":{"highlight":{},"search":{},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false}},"theme":"default","author":"lanyue","pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"variables":{},"title":"Gitbook demo","language":"zh-hans","gitbook":">=3.0.0","description":"this is a test"},"file":{"path":"getters.md","mtime":"2017-02-21T09:41:56.408Z","type":"markdown"},"gitbook":{"version":"3.2.2","time":"2017-03-06T15:58:45.473Z"},"basePath":".","book":{"language":""}});
        });
    </script>
</div>

        
    <script src="gitbook/gitbook.js"></script>
    <script src="gitbook/theme.js"></script>
    
        
        <script src="gitbook/gitbook-plugin-search/search-engine.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-search/search.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-lunr/lunr.min.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-lunr/search-lunr.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-sharing/buttons.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    

    </body>
</html>

